<?php
/**
 * Created by PhpStorm.
 * User: qingyun
 * Date: 17/2/25
 * Time: 上午9:42
 */
namespace Admin\Controller;
use Common\Controller\AdminBaseController;

class AdController extends AdminBaseController
{
    /**
     *广告列表
     */
    public function index()
    {
        //获取广告也
        $pg  = I('get.p');
        //获取关键词
        $word = I('get.word');
        //判断关键词是否为空，则显示第一页
        $where = empty($word) ? '1=1' : 'fd_ad.ad_name like  "%'.$word.'%"';
        //判断页码是否为空，如果为空则为第一页
        $pg = empty($pg) ? '1=1' : $pg;
        $User = M('Ad'); // 实例化User对象
        // 进行分页数据查询 注意page方法的参数的前面部分是当前的页数使用 $_GET[p]获取
        $list = $User->where($where)->page($pg.',3')->select();
        $this->assign('list',$list);// 赋值数据集
        $count      = $User->where($where)->count();// 查询满足要求的总记录数
        $Page       = new \Think\Page($count,3);// 实例化分页类 传入总记录数和每页显示的记录数
        $show       = $Page->show();// 分页显示输出
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('list',$list);
        $this->assign('title','广告管理');
        $this->display();
    }


    /**
     *添加广告
     */
    public function add()
    {
        if(IS_AJAX  || IS_POST){
            //获取广告类型
            $type = I('post.type');
            //判断广告类型是否为空
            if(empty($type)){
                $this->ajaxReturn(['state'=>1,'msg' => '请选择所属广告分类']);
            }
            //获取广告名
            $name = I('post.ad_name');
            if(empty($name)){
                $this->ajaxReturn(['state'=>1,'msg' => '广告名不能为空']);
            }
            //获取广告的连接
            $url = I('post.link_url');
            if(empty($url)){
                $this->ajaxReturn(['state' =>1,'msg' => '广告链接不能为空']);
            }
            $time = I('post.time');
            $min = $time*3600*24;
            $order = I('post.order');
            //获取上传的图片
            $file = $_FILES['pic'];
            //判断是否上传图片
            if(!empty($file)){
                $upload = new \Think\Upload();// 实例化上传类
                $upload->maxSize   =     3145728 ;// 设置附件上传大小
                //判断文件上传大小
                if($file['size'] > $upload->maxSize){
                    $this->ajaxReturn(['state' => 1 ,'msg' => '上传文件过大']);
                }
                $upload->mimes      =     array('image/jpg', 'image/gif', 'image/png', 'image/jpeg');// 设置附件上传类型
                //判断文件上传类型是否符合规定
                if(!in_array($file['type'],$upload->mimes)){
                    $this->ajaxReturn(['state' => 1 , 'msg' => '上传图片格式不正确']);
                }
                $upload->rootPath  =     'Upload/'; // 设置附件上传根目录
                $upload->savePath  =     'Ad/'; // 设置附件上传（子）目录
                $upload->exts      =        array('jpg','gif','png','jpeg');//设置文件的后缀
                //图片的命名规则
                $upload->saveName  =        uniqid();
                //设置保存图片时图片的后缀类型
                $upload->saveExt   =       'png';
                // 上传文件
                $info   =   $upload->upload();
                if(!$info) {// 上传错误提示错误信息
                    $this->ajaxReturn(['state' => 1 ,'msg' => $upload->getError()]);
                }else{// 上传成功
                    //获取文件的路径
                    $path = '/'.$upload->rootPath.$upload->savePath.date('Y-m-d').'/'.$upload->saveName.'.'.$upload->saveExt;
                    $date = [
                        'ad_name' => $name,
                        'litimg'  => $path,
                        'type'    =>  $type,
                        'link_url' => $url,
                        'sort_order'  => $order,
                        'time'      =>time()+$min
                    ];
                    $info =  M('Ad')->add($date);
                    //判断是否添加成功
                    if(!$info){
                        $this->ajaxReturn(['state' =>1 ,'msg' => '添加失败']);
                    }else{
                        $this->ajaxReturn(['state' => 0 ,'msg' => '添加成功']);
                    }
                }
            }
            //当不上传图片时
//            $date = [
//                'ad_name' => $name,
//                'type'    =>  $type,
//                'link_url' => $url,
//                'sort_order'  => $order
//            ];
//            $info =  M('Ad')->add($date);
//            if(!$info){
                $this->ajaxReturn(['state' => 1,'msg' => '请上传图片']);
//            }
//            $this->ajaxReturn(['state' => 0,'msg'=>'修改成功']);
        }
        $this->assign('title','广告添加');
        $this->display();
    }

    /**
     *删除广告
     */
    public function  del()
    {
        //获取索要删除的广告id
        $id = I('post.id');
        if(empty($id)){
           $this->ajaxReturn(['state' => 1 ,'msg' => '非法操作']);
        }
        $path = M('Ad')->where(['id'=>$id])->getField('litimg');
        if(!$path){
            $this->ajaxReturn(['state' =>1 ,'msg'=>'图片不存在']);
        }
        $ad = M('Ad');
        $ad->startTrans();
        $indel = M('Ad')->where(['id' => $id])->delete();
        if(!$indel){
            $ad->rollback();
            $this->ajaxReturn(['state'=>1,'msg'=>'删除失败']);
        }
        $pic = unlink('.'.$path);
        if(!$pic){
            $ad->rollback();
            $this->ajaxReturn(['state'=>1,'msg'=>'图片删除失败']);
        }
        $ad->commit();
        $this->ajaxReturn(['state' => 0,'msg' => '删除成功']);
    }


    /**
     *查看要修改的广告信息
     */
    public function edit()
    {
        //获取要修改的广告信息
        $id = I('get.id');
        //判断要修改的广告id是否为空
        if(empty($id)){
            $this->ajaxReturn(['state'=>1,'msg' => '非法操作']);
        }
        $info = M('Ad')->where(['id'=>$id])->find();
        if(empty($info)){
            $this->ajaxReturn(['state'=>1,'msg'=>'所要修改的广告信息不存在']);
        }
        $this->assign('list',$info);
        $this->assign('title','修改广告信息');
        $this->display();
    }


    /**
     *修改广告信息
     */
    public function save()
    {
        if(IS_AJAX  || IS_POST){
            $id = I('post.id');
            //获取广告类型
            $type = I('post.type');
            if(empty($id)){
                $this->ajaxReturn(['state' => 1 ,'msg' => '非法操作']);
            }
            //判断广告类型是否为空
            if(empty($type)){
                $this->ajaxReturn(['state'=>1,'msg' => '请选择所属广告分类']);
            }
            //获取广告名
            $name = I('post.ad_name');
            if(empty($name)){
                $this->ajaxReturn(['state'=>1,'msg' => '广告名不能为空']);
            }
            //获取广告的连接
            $url = I('post.link_url');
            if(empty($url)){
                $this->ajaxReturn(['state' =>1,'msg' => '广告链接不能为空']);
            }
            $time = I('post.time');
            $min = $time*3600*24;
            $order = I('post.order');
            //当修改图片是
            //获取上传的图片
            $file = $_FILES['pic'];
            //判断是否上传图片
            if(!empty($file)){
                $path = M('Ad')->where(['id'=>$id])->getField('litimg');
                $pic = unlink('.'.$path);
                if(!$pic){
                    $this->ajaxReturn(['state'=>1,'msg'=>'图片删除失败']);
                }
                $upload = new \Think\Upload();// 实例化上传类
                $upload->maxSize   =     3145728 ;// 设置附件上传大小
                //判断文件上传大小
                if($file['size'] > $upload->maxSize){
                    $this->ajaxReturn(['state' => 1 ,'msg' => '上传文件过大']);
                }
                $upload->mimes      =     array('image/jpg', 'image/gif', 'image/png', 'image/jpeg');// 设置附件上传类型
                //判断文件上传类型是否符合规定
                if(!in_array($file['type'],$upload->mimes)){
                    $this->ajaxReturn(['state' => 1 , 'msg' => '上传图片格式不正确']);
                }
                $upload->rootPath  =     'Upload/'; // 设置附件上传根目录
                $upload->savePath  =     'Ad/'; // 设置附件上传（子）目录
                $upload->exts      =        array('jpg','gif','png','jpeg');//设置文件的后缀
                //图片的命名规则
                $upload->saveName  =        uniqid();
                //设置保存图片时图片的后缀类型
                $upload->saveExt   =       'png';
                // 上传文件
                $info   =   $upload->upload();
                if(!$info) {// 上传错误提示错误信息
                    $this->ajaxReturn(['state' => 1 ,'msg' => $upload->getError()]);
                }else{// 上传成功
                    //获取文件的路径
                    $path = '/'.$upload->rootPath.$upload->savePath.date('Y-m-d').'/'.$upload->saveName.'.'.$upload->saveExt;
                    $date = [
                        'ad_name' => $name,
                        'litimg'  => $path,
                        'type'    =>  $type,
                        'link_url' => $url,
                        'sort_order'  => $order,
                        'time'      =>time()+$min
                    ];
                }
                $info = M('Ad')->where(['id'=>$id])->save($date);
                if(!$info){
                    $this->ajaxReturn(['state' => 1,'msg' => '修改失败']);
                }
                $this->ajaxReturn(['state' => 0,'msg'=>'修改成功']);
            }
            //当不修改图片是
            $info = [
                'ad_name' => $name,
                'type'    =>  $type,
                'link_url' => $url,
                'sort_order'  => $order,
                'time'      =>time()+$min
            ];
            $list = M('Ad')->where(['id'=>$id])->save($info);
            if($list === false){
                $this->ajaxReturn(['state' => 1,'msg' => '修改失败']);
            }
            $this->ajaxReturn(['state' => 0,'msg'=>'修改成功']);
        }
    }

    public function changState()
    {
        if(IS_POST){
            $id = I('post.id');
            if(empty($id)){
                $this->ajaxReturn(['state'=>1,'msg'=>'非法操作']);
            }
            $ad_status = M('Ad')->where(['id'=>$id])->getField('status');
            if($ad_status === false){
                $this->ajaxReturn(['state' => 1 ,'msg' => '修改失败']);
            }
            if($ad_status){
                M('Ad')->where(['id'=>$id])->setField('status','0');
                $this->ajaxReturn(['state' =>0,'msg' => '已禁止','data'=>1]);
            }else{
                M('Ad')->where(['id'=>$id])->setField('status','1');
                $this->ajaxReturn(['state'=>0 ,'msg'=>'已开启','data'=>0]);
            }
        }else{
            $this->ajaxReturn(['state'=>1,'msg'=>'非法操作']);
        }
    }

}